<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perlootut - Object-Oriented Programming in Perl Tutorial - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perlootut">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlootut.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perlootut">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perlootut">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perlootut">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perlootut">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perlootut">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perlootut">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perlootut">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perlootut">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perlootut">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perlootut">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perlootut">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perlootut">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perlootut">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perlootut">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perlootut">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perlootut">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perlootut">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perlootut">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perlootut">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perlootut">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perlootut">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perlootut">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perlootut">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perlootut">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perlootut">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perlootut">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perlootut">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perlootut">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perlootut">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perlootut">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perlootut">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perlootut">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perlootut">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perlootut">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perlootut">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perlootut">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perlootut">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perlootut">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perlootut">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perlootut">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perlootut">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perlootut">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perlootut">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perlootut">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perlootut">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perlootut">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perlootut">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perlootut">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perlootut">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perlootut">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perlootut">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perlootut">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perlootut">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perlootut">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perlootut">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perlootut">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perlootut">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perlootut">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perlootut">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perlootut">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perlootut">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perlootut">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perlootut">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perlootut">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perlootut">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perlootut">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perlootut">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perlootut">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perlootut">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perlootut">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perlootut">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perlootut">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perlootut">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perlootut">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perlootut">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perlootut">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perlootut">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perlootut">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perlootut">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perlootut">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlootut.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perlootut">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perlootut">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perlootut">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perlootut">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perlootut">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perlootut">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perlootut">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perlootut">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perlootut">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perlootut">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perlootut">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perlootut">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perlootut">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perlootut">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perlootut">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perlootut">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perlootut">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perlootut">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perlootut">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perlootut">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perlootut">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perlootut">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perlootut">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perlootut">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perlootut">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perlootut">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perlootut">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perlootut">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perlootut">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perlootut">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perlootut">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perlootut">5.35.1</a>
          <a class="dropdown-item active" href="perlootut.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlootut.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perlootut.html">perlootut</a>
          <div id="more">
            (<a href="perlootut.txt">source</a>,
            <a href="https://metacpan.org/pod/perlootut">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perlootut.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#DATE">DATE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#OBJECT-ORIENTED-FUNDAMENTALS">OBJECT-ORIENTED FUNDAMENTALS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Object">Object</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Class">Class</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Blessing">Blessing</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Constructor">Constructor</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Methods">Methods</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Attributes">Attributes</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Polymorphism">Polymorphism</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Inheritance">Inheritance</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Overriding-methods-and-method-resolution">Overriding methods and method resolution</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Encapsulation">Encapsulation</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Composition">Composition</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Roles">Roles</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#When-to-Use-OO">When to Use OO</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#PERL-OO-SYSTEMS">PERL OO SYSTEMS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Moose">Moose</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Moo">Moo</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Class::Accessor">Class::Accessor</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Class::Tiny">Class::Tiny</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Role::Tiny">Role::Tiny</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#OO-System-Summary">OO System Summary</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#Other-OO-Systems">Other OO Systems</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlootut.html#CONCLUSION">CONCLUSION</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perlootut.html#NAME">#</a>NAME</h1>

<p>perlootut - Object-Oriented Programming in Perl Tutorial</p>

<h1 id="DATE"><a class="permalink" href="perlootut.html#DATE">#</a>DATE</h1>

<p>This document was created in February, 2011, and the last major revision was in February, 2013.</p>

<p>If you are reading this in the future then it&#39;s possible that the state of the art has changed. We recommend you start by reading the perlootut document in the latest stable release of Perl, rather than this version.</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perlootut.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>This document provides an introduction to object-oriented programming in Perl. It begins with a brief overview of the concepts behind object oriented design. Then it introduces several different OO systems from <a href="https://www.cpan.org">CPAN</a> which build on top of what Perl provides.</p>

<p>By default, Perl&#39;s built-in OO system is very minimal, leaving you to do most of the work. This minimalism made a lot of sense in 1994, but in the years since Perl 5.0 we&#39;ve seen a number of common patterns emerge in Perl OO. Fortunately, Perl&#39;s flexibility has allowed a rich ecosystem of Perl OO systems to flourish.</p>

<p>If you want to know how Perl OO works under the hood, the <a href="perlobj.html">perlobj</a> document explains the nitty gritty details.</p>

<p>This document assumes that you already understand the basics of Perl syntax, variable types, operators, and subroutine calls. If you don&#39;t understand these concepts yet, please read <a href="perlintro.html">perlintro</a> first. You should also read the <a href="perlsyn.html">perlsyn</a>, <a href="perlop.html">perlop</a>, and <a href="perlsub.html">perlsub</a> documents.</p>

<h1 id="OBJECT-ORIENTED-FUNDAMENTALS"><a class="permalink" href="perlootut.html#OBJECT-ORIENTED-FUNDAMENTALS">#</a><a id="OBJECT"></a>OBJECT-ORIENTED FUNDAMENTALS</h1>

<p>Most object systems share a number of common concepts. You&#39;ve probably heard terms like &quot;class&quot;, &quot;object, &quot;method&quot;, and &quot;attribute&quot; before. Understanding the concepts will make it much easier to read and write object-oriented code. If you&#39;re already familiar with these terms, you should still skim this section, since it explains each concept in terms of Perl&#39;s OO implementation.</p>

<p>Perl&#39;s OO system is class-based. Class-based OO is fairly common. It&#39;s used by Java, C++, C#, Python, Ruby, and many other languages. There are other object orientation paradigms as well. JavaScript is the most popular language to use another paradigm. JavaScript&#39;s OO system is prototype-based.</p>

<h2 id="Object"><a class="permalink" href="perlootut.html#Object">#</a>Object</h2>

<p>An <b>object</b> is a data structure that bundles together data and subroutines which operate on that data. An object&#39;s data is called <b>attributes</b>, and its subroutines are called <b>methods</b>. An object can be thought of as a noun (a person, a web service, a computer).</p>

<p>An object represents a single discrete thing. For example, an object might represent a file. The attributes for a file object might include its path, content, and last modification time. If we created an object to represent <i>/etc/hostname</i> on a machine named &quot;foo.example.com&quot;, that object&#39;s path would be &quot;/etc/hostname&quot;, its content would be &quot;foo\n&quot;, and it&#39;s last modification time would be 1304974868 seconds since the beginning of the epoch.</p>

<p>The methods associated with a file might include <code>rename()</code> and <code>write()</code>.</p>

<p>In Perl most objects are hashes, but the OO systems we recommend keep you from having to worry about this. In practice, it&#39;s best to consider an object&#39;s internal data structure opaque.</p>

<h2 id="Class"><a class="permalink" href="perlootut.html#Class">#</a>Class</h2>

<p>A <b>class</b> defines the behavior of a category of objects. A class is a name for a category (like &quot;File&quot;), and a class also defines the behavior of objects in that category.</p>

<p>All objects belong to a specific class. For example, our <i>/etc/hostname</i> object belongs to the <code>File</code> class. When we want to create a specific object, we start with its class, and <b>construct</b> or <b>instantiate</b> an object. A specific object is often referred to as an <b>instance</b> of a class.</p>

<p>In Perl, any package can be a class. The difference between a package which is a class and one which isn&#39;t is based on how the package is used. Here&#39;s our &quot;class declaration&quot; for the <code>File</code> class:</p>

<pre><code>package File;</code></pre>

<p>In Perl, there is no special keyword for constructing an object. However, most OO modules on CPAN use a method named <code>new()</code> to construct a new object:</p>

<pre><code>my $hostname = File-&gt;new(
    path          =&gt; &#39;/etc/hostname&#39;,
    content       =&gt; &quot;foo\n&quot;,
    last_mod_time =&gt; 1304974868,
);</code></pre>

<p>(Don&#39;t worry about that <code>-&gt;</code> operator, it will be explained later.)</p>

<h3 id="Blessing"><a class="permalink" href="perlootut.html#Blessing">#</a>Blessing</h3>

<p>As we said earlier, most Perl objects are hashes, but an object can be an instance of any Perl data type (scalar, array, etc.). Turning a plain data structure into an object is done by <b>blessing</b> that data structure using Perl&#39;s <code>bless</code> function.</p>

<p>While we strongly suggest you don&#39;t build your objects from scratch, you should know the term <b>bless</b>. A <b>blessed</b> data structure (aka &quot;a referent&quot;) is an object. We sometimes say that an object has been &quot;blessed into a class&quot;.</p>

<p>Once a referent has been blessed, the <code>blessed</code> function from the <a href="./Scalar::Util.html">Scalar::Util</a> core module can tell us its class name. This subroutine returns an object&#39;s class when passed an object, and false otherwise.</p>

<pre><code>use Scalar::Util &#39;blessed&#39;;

print blessed($hash);      # undef
print blessed($hostname);  # File</code></pre>

<h3 id="Constructor"><a class="permalink" href="perlootut.html#Constructor">#</a>Constructor</h3>

<p>A <b>constructor</b> creates a new object. In Perl, a class&#39;s constructor is just another method, unlike some other languages, which provide syntax for constructors. Most Perl classes use <code>new</code> as the name for their constructor:</p>

<pre><code>my $file = File-&gt;new(...);</code></pre>

<h2 id="Methods"><a class="permalink" href="perlootut.html#Methods">#</a>Methods</h2>

<p>You already learned that a <b>method</b> is a subroutine that operates on an object. You can think of a method as the things that an object can <i>do</i>. If an object is a noun, then methods are its verbs (save, print, open).</p>

<p>In Perl, methods are simply subroutines that live in a class&#39;s package. Methods are always written to receive the object as their first argument:</p>

<pre><code>sub print_info {
    my $self = shift;

    print &quot;This file is at &quot;, $self-&gt;path, &quot;\n&quot;;
}

$file-&gt;print_info;
# The file is at /etc/hostname</code></pre>

<p>What makes a method special is <i>how it&#39;s called</i>. The arrow operator (<code>-&gt;</code>) tells Perl that we are calling a method.</p>

<p>When we make a method call, Perl arranges for the method&#39;s <b>invocant</b> to be passed as the first argument. <b>Invocant</b> is a fancy name for the thing on the left side of the arrow. The invocant can either be a class name or an object. We can also pass additional arguments to the method:</p>

<pre><code>sub print_info {
    my $self   = shift;
    my $prefix = shift // &quot;This file is at &quot;;

    print $prefix, &quot;, &quot;, $self-&gt;path, &quot;\n&quot;;
}

$file-&gt;print_info(&quot;The file is located at &quot;);
# The file is located at /etc/hostname</code></pre>

<h2 id="Attributes"><a class="permalink" href="perlootut.html#Attributes">#</a>Attributes</h2>

<p>Each class can define its <b>attributes</b>. When we instantiate an object, we assign values to those attributes. For example, every <code>File</code> object has a path. Attributes are sometimes called <b>properties</b>.</p>

<p>Perl has no special syntax for attributes. Under the hood, attributes are often stored as keys in the object&#39;s underlying hash, but don&#39;t worry about this.</p>

<p>We recommend that you only access attributes via <b>accessor</b> methods. These are methods that can get or set the value of each attribute. We saw this earlier in the <code>print_info()</code> example, which calls <code>$self-&gt;path</code>.</p>

<p>You might also see the terms <b>getter</b> and <b>setter</b>. These are two types of accessors. A getter gets the attribute&#39;s value, while a setter sets it. Another term for a setter is <b>mutator</b></p>

<p>Attributes are typically defined as read-only or read-write. Read-only attributes can only be set when the object is first created, while read-write attributes can be altered at any time.</p>

<p>The value of an attribute may itself be another object. For example, instead of returning its last mod time as a number, the <code>File</code> class could return a <a href="DateTime.html">DateTime</a> object representing that value.</p>

<p>It&#39;s possible to have a class that does not expose any publicly settable attributes. Not every class has attributes and methods.</p>

<h2 id="Polymorphism"><a class="permalink" href="perlootut.html#Polymorphism">#</a>Polymorphism</h2>

<p><b>Polymorphism</b> is a fancy way of saying that objects from two different classes share an API. For example, we could have <code>File</code> and <code>WebPage</code> classes which both have a <code>print_content()</code> method. This method might produce different output for each class, but they share a common interface.</p>

<p>While the two classes may differ in many ways, when it comes to the <code>print_content()</code> method, they are the same. This means that we can try to call the <code>print_content()</code> method on an object of either class, and <b>we don&#39;t have to know what class the object belongs to!</b></p>

<p>Polymorphism is one of the key concepts of object-oriented design.</p>

<h2 id="Inheritance"><a class="permalink" href="perlootut.html#Inheritance">#</a>Inheritance</h2>

<p><b>Inheritance</b> lets you create a specialized version of an existing class. Inheritance lets the new class reuse the methods and attributes of another class.</p>

<p>For example, we could create an <code>File::MP3</code> class which <b>inherits</b> from <code>File</code>. An <code>File::MP3</code> <b>is-a</b> <i>more specific</i> type of <code>File</code>. All mp3 files are files, but not all files are mp3 files.</p>

<p>We often refer to inheritance relationships as <b>parent-child</b> or <code>superclass</code>/<code>subclass</code> relationships. Sometimes we say that the child has an <b>is-a</b> relationship with its parent class.</p>

<p><code>File</code> is a <b>superclass</b> of <code>File::MP3</code>, and <code>File::MP3</code> is a <b>subclass</b> of <code>File</code>.</p>

<pre><code>package File::MP3;

use parent &#39;File&#39;;</code></pre>

<p>The <a href="parent.html">parent</a> module is one of several ways that Perl lets you define inheritance relationships.</p>

<p>Perl allows multiple inheritance, which means that a class can inherit from multiple parents. While this is possible, we strongly recommend against it. Generally, you can use <b>roles</b> to do everything you can do with multiple inheritance, but in a cleaner way.</p>

<p>Note that there&#39;s nothing wrong with defining multiple subclasses of a given class. This is both common and safe. For example, we might define <code>File::MP3::FixedBitrate</code> and <code>File::MP3::VariableBitrate</code> classes to distinguish between different types of mp3 file.</p>

<h3 id="Overriding-methods-and-method-resolution"><a class="permalink" href="perlootut.html#Overriding-methods-and-method-resolution">#</a><a id="Overriding"></a>Overriding methods and method resolution</h3>

<p>Inheritance allows two classes to share code. By default, every method in the parent class is also available in the child. The child can explicitly <b>override</b> a parent&#39;s method to provide its own implementation. For example, if we have an <code>File::MP3</code> object, it has the <code>print_info()</code> method from <code>File</code>:</p>

<pre><code>my $cage = File::MP3-&gt;new(
    path          =&gt; &#39;mp3s/My-Body-Is-a-Cage.mp3&#39;,
    content       =&gt; $mp3_data,
    last_mod_time =&gt; 1304974868,
    title         =&gt; &#39;My Body Is a Cage&#39;,
);

$cage-&gt;print_info;
# The file is at mp3s/My-Body-Is-a-Cage.mp3</code></pre>

<p>If we wanted to include the mp3&#39;s title in the greeting, we could override the method:</p>

<pre><code>package File::MP3;

use parent &#39;File&#39;;

sub print_info {
    my $self = shift;

    print &quot;This file is at &quot;, $self-&gt;path, &quot;\n&quot;;
    print &quot;Its title is &quot;, $self-&gt;title, &quot;\n&quot;;
}

$cage-&gt;print_info;
# The file is at mp3s/My-Body-Is-a-Cage.mp3
# Its title is My Body Is a Cage</code></pre>

<p>The process of determining what method should be used is called <b>method resolution</b>. What Perl does is look at the object&#39;s class first (<code>File::MP3</code> in this case). If that class defines the method, then that class&#39;s version of the method is called. If not, Perl looks at each parent class in turn. For <code>File::MP3</code>, its only parent is <code>File</code>. If <code>File::MP3</code> does not define the method, but <code>File</code> does, then Perl calls the method in <code>File</code>.</p>

<p>If <code>File</code> inherited from <code>DataSource</code>, which inherited from <code>Thing</code>, then Perl would keep looking &quot;up the chain&quot; if necessary.</p>

<p>It is possible to explicitly call a parent method from a child:</p>

<pre><code>package File::MP3;

use parent &#39;File&#39;;

sub print_info {
    my $self = shift;

    $self-&gt;SUPER::print_info();
    print &quot;Its title is &quot;, $self-&gt;title, &quot;\n&quot;;
}</code></pre>

<p>The <code>SUPER::</code> bit tells Perl to look for the <code>print_info()</code> in the <code>File::MP3</code> class&#39;s inheritance chain. When it finds the parent class that implements this method, the method is called.</p>

<p>We mentioned multiple inheritance earlier. The main problem with multiple inheritance is that it greatly complicates method resolution. See <a href="perlobj.html">perlobj</a> for more details.</p>

<h2 id="Encapsulation"><a class="permalink" href="perlootut.html#Encapsulation">#</a>Encapsulation</h2>

<p><b>Encapsulation</b> is the idea that an object is opaque. When another developer uses your class, they don&#39;t need to know <i>how</i> it is implemented, they just need to know <i>what</i> it does.</p>

<p>Encapsulation is important for several reasons. First, it allows you to separate the public API from the private implementation. This means you can change that implementation without breaking the API.</p>

<p>Second, when classes are well encapsulated, they become easier to subclass. Ideally, a subclass uses the same APIs to access object data that its parent class uses. In reality, subclassing sometimes involves violating encapsulation, but a good API can minimize the need to do this.</p>

<p>We mentioned earlier that most Perl objects are implemented as hashes under the hood. The principle of encapsulation tells us that we should not rely on this. Instead, we should use accessor methods to access the data in that hash. The object systems that we recommend below all automate the generation of accessor methods. If you use one of them, you should never have to access the object as a hash directly.</p>

<h2 id="Composition"><a class="permalink" href="perlootut.html#Composition">#</a>Composition</h2>

<p>In object-oriented code, we often find that one object references another object. This is called <b>composition</b>, or a <b>has-a</b> relationship.</p>

<p>Earlier, we mentioned that the <code>File</code> class&#39;s <code>last_mod_time</code> accessor could return a <a href="DateTime.html">DateTime</a> object. This is a perfect example of composition. We could go even further, and make the <code>path</code> and <code>content</code> accessors return objects as well. The <code>File</code> class would then be <b>composed</b> of several other objects.</p>

<h2 id="Roles"><a class="permalink" href="perlootut.html#Roles">#</a>Roles</h2>

<p><b>Roles</b> are something that a class <i>does</i>, rather than something that it <i>is</i>. Roles are relatively new to Perl, but have become rather popular. Roles are <b>applied</b> to classes. Sometimes we say that classes <b>consume</b> roles.</p>

<p>Roles are an alternative to inheritance for providing polymorphism. Let&#39;s assume we have two classes, <code>Radio</code> and <code>Computer</code>. Both of these things have on/off switches. We want to model that in our class definitions.</p>

<p>We could have both classes inherit from a common parent, like <code>Machine</code>, but not all machines have on/off switches. We could create a parent class called <code>HasOnOffSwitch</code>, but that is very artificial. Radios and computers are not specializations of this parent. This parent is really a rather ridiculous creation.</p>

<p>This is where roles come in. It makes a lot of sense to create a <code>HasOnOffSwitch</code> role and apply it to both classes. This role would define a known API like providing <code>turn_on()</code> and <code>turn_off()</code> methods.</p>

<p>Perl does not have any built-in way to express roles. In the past, people just bit the bullet and used multiple inheritance. Nowadays, there are several good choices on CPAN for using roles.</p>

<h2 id="When-to-Use-OO"><a class="permalink" href="perlootut.html#When-to-Use-OO">#</a><a id="When"></a>When to Use OO</h2>

<p>Object Orientation is not the best solution to every problem. In <i>Perl Best Practices</i> (copyright 2004, Published by O&#39;Reilly Media, Inc.), Damian Conway provides a list of criteria to use when deciding if OO is the right fit for your problem:</p>

<ul>

<li><p>The system being designed is large, or is likely to become large.</p>

</li>
<li><p>The data can be aggregated into obvious structures, especially if there&#39;s a large amount of data in each aggregate.</p>

</li>
<li><p>The various types of data aggregate form a natural hierarchy that facilitates the use of inheritance and polymorphism.</p>

</li>
<li><p>You have a piece of data on which many different operations are applied.</p>

</li>
<li><p>You need to perform the same general operations on related types of data, but with slight variations depending on the specific type of data the operations are applied to.</p>

</li>
<li><p>It&#39;s likely you&#39;ll have to add new data types later.</p>

</li>
<li><p>The typical interactions between pieces of data are best represented by operators.</p>

</li>
<li><p>The implementation of individual components of the system is likely to change over time.</p>

</li>
<li><p>The system design is already object-oriented.</p>

</li>
<li><p>Large numbers of other programmers will be using your code modules.</p>

</li>
</ul>

<h1 id="PERL-OO-SYSTEMS"><a class="permalink" href="perlootut.html#PERL-OO-SYSTEMS">#</a><a id="PERL"></a>PERL OO SYSTEMS</h1>

<p>As we mentioned before, Perl&#39;s built-in OO system is very minimal, but also quite flexible. Over the years, many people have developed systems which build on top of Perl&#39;s built-in system to provide more features and convenience.</p>

<p>We strongly recommend that you use one of these systems. Even the most minimal of them eliminates a lot of repetitive boilerplate. There&#39;s really no good reason to write your classes from scratch in Perl.</p>

<p>If you are interested in the guts underlying these systems, check out <a href="perlobj.html">perlobj</a>.</p>

<h2 id="Moose"><a class="permalink" href="perlootut.html#Moose">#</a>Moose</h2>

<p><a href="Moose.html">Moose</a> bills itself as a &quot;postmodern object system for Perl 5&quot;. Don&#39;t be scared, the &quot;postmodern&quot; label is a callback to Larry&#39;s description of Perl as &quot;the first postmodern computer language&quot;.</p>

<p><code>Moose</code> provides a complete, modern OO system. Its biggest influence is the Common Lisp Object System, but it also borrows ideas from Smalltalk and several other languages. <code>Moose</code> was created by Stevan Little, and draws heavily from his work on the Raku OO design.</p>

<p>Here is our <code>File</code> class using <code>Moose</code>:</p>

<pre><code>package File;
use Moose;

has path          =&gt; ( is =&gt; &#39;ro&#39; );
has content       =&gt; ( is =&gt; &#39;ro&#39; );
has last_mod_time =&gt; ( is =&gt; &#39;ro&#39; );

sub print_info {
    my $self = shift;

    print &quot;This file is at &quot;, $self-&gt;path, &quot;\n&quot;;
}</code></pre>

<p><code>Moose</code> provides a number of features:</p>

<ul>

<li><p>Declarative sugar</p>

<p><code>Moose</code> provides a layer of declarative &quot;sugar&quot; for defining classes. That sugar is just a set of exported functions that make declaring how your class works simpler and more palatable. This lets you describe <i>what</i> your class is, rather than having to tell Perl <i>how</i> to implement your class.</p>

<p>The <code>has()</code> subroutine declares an attribute, and <code>Moose</code> automatically creates accessors for these attributes. It also takes care of creating a <code>new()</code> method for you. This constructor knows about the attributes you declared, so you can set them when creating a new <code>File</code>.</p>

</li>
<li><p>Roles built-in</p>

<p><code>Moose</code> lets you define roles the same way you define classes:</p>

<pre><code>package HasOnOffSwitch;
use Moose::Role;

has is_on =&gt; (
    is  =&gt; &#39;rw&#39;,
    isa =&gt; &#39;Bool&#39;,
);

sub turn_on {
    my $self = shift;
    $self-&gt;is_on(1);
}

sub turn_off {
    my $self = shift;
    $self-&gt;is_on(0);
}</code></pre>

</li>
<li><p>A miniature type system</p>

<p>In the example above, you can see that we passed <code>isa =&gt; &#39;Bool&#39;</code> to <code>has()</code> when creating our <code>is_on</code> attribute. This tells <code>Moose</code> that this attribute must be a boolean value. If we try to set it to an invalid value, our code will throw an error.</p>

</li>
<li><p>Full introspection and manipulation</p>

<p>Perl&#39;s built-in introspection features are fairly minimal. <code>Moose</code> builds on top of them and creates a full introspection layer for your classes. This lets you ask questions like &quot;what methods does the File class implement?&quot; It also lets you modify your classes programmatically.</p>

</li>
<li><p>Self-hosted and extensible</p>

<p><code>Moose</code> describes itself using its own introspection API. Besides being a cool trick, this means that you can extend <code>Moose</code> using <code>Moose</code> itself.</p>

</li>
<li><p>Rich ecosystem</p>

<p>There is a rich ecosystem of <code>Moose</code> extensions on CPAN under the <a href="https://metacpan.org/search?q=MooseX">MooseX</a> namespace. In addition, many modules on CPAN already use <code>Moose</code>, providing you with lots of examples to learn from.</p>

</li>
<li><p>Many more features</p>

<p><code>Moose</code> is a very powerful tool, and we can&#39;t cover all of its features here. We encourage you to learn more by reading the <code>Moose</code> documentation, starting with <a href="https://metacpan.org/pod/Moose::Manual">Moose::Manual</a>.</p>

</li>
</ul>

<p>Of course, <code>Moose</code> isn&#39;t perfect.</p>

<p><code>Moose</code> can make your code slower to load. <code>Moose</code> itself is not small, and it does a <i>lot</i> of code generation when you define your class. This code generation means that your runtime code is as fast as it can be, but you pay for this when your modules are first loaded.</p>

<p>This load time hit can be a problem when startup speed is important, such as with a command-line script or a &quot;plain vanilla&quot; CGI script that must be loaded each time it is executed.</p>

<p>Before you panic, know that many people do use <code>Moose</code> for command-line tools and other startup-sensitive code. We encourage you to try <code>Moose</code> out first before worrying about startup speed.</p>

<p><code>Moose</code> also has several dependencies on other modules. Most of these are small stand-alone modules, a number of which have been spun off from <code>Moose</code>. <code>Moose</code> itself, and some of its dependencies, require a compiler. If you need to install your software on a system without a compiler, or if having <i>any</i> dependencies is a problem, then <code>Moose</code> may not be right for you.</p>

<h3 id="Moo"><a class="permalink" href="perlootut.html#Moo">#</a>Moo</h3>

<p>If you try <code>Moose</code> and find that one of these issues is preventing you from using <code>Moose</code>, we encourage you to consider <a href="Moo.html">Moo</a> next. <code>Moo</code> implements a subset of <code>Moose</code>&#39;s functionality in a simpler package. For most features that it does implement, the end-user API is <i>identical</i> to <code>Moose</code>, meaning you can switch from <code>Moo</code> to <code>Moose</code> quite easily.</p>

<p><code>Moo</code> does not implement most of <code>Moose</code>&#39;s introspection API, so it&#39;s often faster when loading your modules. Additionally, none of its dependencies require XS, so it can be installed on machines without a compiler.</p>

<p>One of <code>Moo</code>&#39;s most compelling features is its interoperability with <code>Moose</code>. When someone tries to use <code>Moose</code>&#39;s introspection API on a <code>Moo</code> class or role, it is transparently inflated into a <code>Moose</code> class or role. This makes it easier to incorporate <code>Moo</code>-using code into a <code>Moose</code> code base and vice versa.</p>

<p>For example, a <code>Moose</code> class can subclass a <code>Moo</code> class using <code>extends</code> or consume a <code>Moo</code> role using <code>with</code>.</p>

<p>The <code>Moose</code> authors hope that one day <code>Moo</code> can be made obsolete by improving <code>Moose</code> enough, but for now it provides a worthwhile alternative to <code>Moose</code>.</p>

<h2 id="Class::Accessor"><a class="permalink" href="perlootut.html#Class::Accessor">#</a><a id="Class1"></a>Class::Accessor</h2>

<p><a href="./Class::Accessor.html">Class::Accessor</a> is the polar opposite of <code>Moose</code>. It provides very few features, nor is it self-hosting.</p>

<p>It is, however, very simple, pure Perl, and it has no non-core dependencies. It also provides a &quot;Moose-like&quot; API on demand for the features it supports.</p>

<p>Even though it doesn&#39;t do much, it is still preferable to writing your own classes from scratch.</p>

<p>Here&#39;s our <code>File</code> class with <code>Class::Accessor</code>:</p>

<pre><code>package File;
use Class::Accessor &#39;antlers&#39;;

has path          =&gt; ( is =&gt; &#39;ro&#39; );
has content       =&gt; ( is =&gt; &#39;ro&#39; );
has last_mod_time =&gt; ( is =&gt; &#39;ro&#39; );

sub print_info {
    my $self = shift;

    print &quot;This file is at &quot;, $self-&gt;path, &quot;\n&quot;;
}</code></pre>

<p>The <code>antlers</code> import flag tells <code>Class::Accessor</code> that you want to define your attributes using <code>Moose</code>-like syntax. The only parameter that you can pass to <code>has</code> is <code>is</code>. We recommend that you use this Moose-like syntax if you choose <code>Class::Accessor</code> since it means you will have a smoother upgrade path if you later decide to move to <code>Moose</code>.</p>

<p>Like <code>Moose</code>, <code>Class::Accessor</code> generates accessor methods and a constructor for your class.</p>

<h2 id="Class::Tiny"><a class="permalink" href="perlootut.html#Class::Tiny">#</a><a id="Class2"></a>Class::Tiny</h2>

<p>Finally, we have <a href="./Class::Tiny.html">Class::Tiny</a>. This module truly lives up to its name. It has an incredibly minimal API and absolutely no dependencies on any recent Perl. Still, we think it&#39;s a lot easier to use than writing your own OO code from scratch.</p>

<p>Here&#39;s our <code>File</code> class once more:</p>

<pre><code>package File;
use Class::Tiny qw( path content last_mod_time );

sub print_info {
    my $self = shift;

    print &quot;This file is at &quot;, $self-&gt;path, &quot;\n&quot;;
}</code></pre>

<p>That&#39;s it!</p>

<p>With <code>Class::Tiny</code>, all accessors are read-write. It generates a constructor for you, as well as the accessors you define.</p>

<p>You can also use <a href="./Class::Tiny::Antlers.html">Class::Tiny::Antlers</a> for <code>Moose</code>-like syntax.</p>

<h2 id="Role::Tiny"><a class="permalink" href="perlootut.html#Role::Tiny">#</a><a id="Role"></a>Role::Tiny</h2>

<p>As we mentioned before, roles provide an alternative to inheritance, but Perl does not have any built-in role support. If you choose to use Moose, it comes with a full-fledged role implementation. However, if you use one of our other recommended OO modules, you can still use roles with <a href="./Role::Tiny.html">Role::Tiny</a></p>

<p><code>Role::Tiny</code> provides some of the same features as Moose&#39;s role system, but in a much smaller package. Most notably, it doesn&#39;t support any sort of attribute declaration, so you have to do that by hand. Still, it&#39;s useful, and works well with <code>Class::Accessor</code> and <code>Class::Tiny</code></p>

<h2 id="OO-System-Summary"><a class="permalink" href="perlootut.html#OO-System-Summary">#</a><a id="OO"></a>OO System Summary</h2>

<p>Here&#39;s a brief recap of the options we covered:</p>

<ul>

<li><p><a href="Moose.html">Moose</a></p>

<p><code>Moose</code> is the maximal option. It has a lot of features, a big ecosystem, and a thriving user base. We also covered <a href="Moo.html">Moo</a> briefly. <code>Moo</code> is <code>Moose</code> lite, and a reasonable alternative when Moose doesn&#39;t work for your application.</p>

</li>
<li><p><a href="./Class::Accessor.html">Class::Accessor</a></p>

<p><code>Class::Accessor</code> does a lot less than <code>Moose</code>, and is a nice alternative if you find <code>Moose</code> overwhelming. It&#39;s been around a long time and is well battle-tested. It also has a minimal <code>Moose</code> compatibility mode which makes moving from <code>Class::Accessor</code> to <code>Moose</code> easy.</p>

</li>
<li><p><a href="./Class::Tiny.html">Class::Tiny</a></p>

<p><code>Class::Tiny</code> is the absolute minimal option. It has no dependencies, and almost no syntax to learn. It&#39;s a good option for a super minimal environment and for throwing something together quickly without having to worry about details.</p>

</li>
<li><p><a href="./Role::Tiny.html">Role::Tiny</a></p>

<p>Use <code>Role::Tiny</code> with <code>Class::Accessor</code> or <code>Class::Tiny</code> if you find yourself considering multiple inheritance. If you go with <code>Moose</code>, it comes with its own role implementation.</p>

</li>
</ul>

<h2 id="Other-OO-Systems"><a class="permalink" href="perlootut.html#Other-OO-Systems">#</a><a id="Other"></a>Other OO Systems</h2>

<p>There are literally dozens of other OO-related modules on CPAN besides those covered here, and you&#39;re likely to run across one or more of them if you work with other people&#39;s code.</p>

<p>In addition, plenty of code in the wild does all of its OO &quot;by hand&quot;, using just the Perl built-in OO features. If you need to maintain such code, you should read <a href="perlobj.html">perlobj</a> to understand exactly how Perl&#39;s built-in OO works.</p>

<h1 id="CONCLUSION"><a class="permalink" href="perlootut.html#CONCLUSION">#</a>CONCLUSION</h1>

<p>As we said before, Perl&#39;s minimal OO system has led to a profusion of OO systems on CPAN. While you can still drop down to the bare metal and write your classes by hand, there&#39;s really no reason to do that with modern Perl.</p>

<p>For small systems, <a href="./Class::Tiny.html">Class::Tiny</a> and <a href="./Class::Accessor.html">Class::Accessor</a> both provide minimal object systems that take care of basic boilerplate for you.</p>

<p>For bigger projects, <a href="Moose.html">Moose</a> provides a rich set of features that will let you focus on implementing your business logic. <a href="Moo.html">Moo</a> provides a nice alternative to <a href="Moose.html">Moose</a> when you want a lot of features but need faster compile time or to avoid XS.</p>

<p>We encourage you to play with and evaluate <a href="Moose.html">Moose</a>, <a href="Moo.html">Moo</a>, <a href="./Class::Accessor.html">Class::Accessor</a>, and <a href="./Class::Tiny.html">Class::Tiny</a> to see which OO system is right for you.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
